// 对于书籍的操作

var exp = require('express')
var multer = require('multer')
var { Book, Car } = require('../lib/monogse')
var fs = require('fs')

var router = exp.Router()

var storage = multer.diskStorage({
  destination:'www/bookimg',
  filename:function(req,file,callback) {
    // 判断服务器端的资源中有没有该书籍的图片
     var files = fs.readdirSync('www/bookimg')
     files.forEach(file=>{
       if(file.startsWith(req.query.bookname)){
         fs.unlinkSync('www/bookimg/'+file)
       }
     })

    var name = req.query.bookname+'.'+file.originalname.split('.')[1]
    req.body.pic=name
    callback(null,name)
  }
})
var upload = multer({storage})

// 添加书籍
router.post('/addbook',upload.single('pic'),(req,res)=>{
  console.log(req.body);
  // 对书籍信息进行存储的时候，要先判断该书籍是否存在
  Book.findOne({bookname:req.body.bookname},(err,data)=>{
    if(!err){
      if(data){ 
        // 该书籍存在
        res.send({code:0,msg:'该书籍已存在'})
      }else{
        var book = new Book(req.body)
        book.save(err=>{
          if(!err){
             res.send({code:1,msg:'添加成功'})
          }
        })
      }
    }
  })
})
// 修改书籍
router.post('/updatebookData',upload.single('pic'),(req,res)=>{ 
  Book.updateOne({_id:req.query.id},req.body,err=>{
    if(!err){
        res.send({code:1,msg:'修改成功'})
    }
  })
})

// 删除书籍
router.get('/removebook',(req,res)=>{
   Book.deleteOne({_id:req.query.id},err=>{
     if(!err){
       res.send('删除成功')
     }
   })
})

// 添加购物车
router.get('/addcar',(req,res)=>{
  var bookobj={};
  Book.findOne({_id:req.query.id},(err,data)=>{
    if(!err){
      // 不能直接对数据库数据（对象的深度复制问题）进行修改
      // 要先把数据进行转化为Json格式，才能对数据进行修改
      // console.log(data.author);
      bookobj.username=req.query.username;
      bookobj.author=data.author;
      bookobj.pic=data.pic;
      bookobj.price=data.price*data.count/10;
      bookobj.bookname=data.bookname;
      
      Car.findOne({bookname:data.bookname,username:req.query.username},(err,book)=>{
        if(!err){
          bookobj.sum=book?book.sum*1+1:1;
          bookobj.nowprice=data.price*(data.count/10)*bookobj.sum

          if(book){
            // 改数据
            Car.updateOne({bookname:data.bookname,username:req.query.username},bookobj,err=>{
              if(!err){
                res.send('添加购物车成功')
              }
            })
          }else{
            // 存数据
            var addcar=new Car(bookobj)
            addcar.save(err=>{
              if(!err){
                res.send('添加购物车成功')
              }else{
                console.log(err);
              }
            })
          }
        }
      })
    }else{
      console.log(err);
    }
  })
})
router.post('/updatebooknum',(req,res)=>{
  console.log(req.body);
  Car.updateOne({bookname:req.body.bookname,username:req.body.username},req.body,err=>{
    if(!err){
      res.send({code:1,msg:'修改成功'})
    }
  })
})
// 移除订单
router.get('/removeshopcar',(req,res)=>{
  Car.deleteOne({bookname:req.query.bookname,username:req.query.username},err=>{
    if(!err){
      res.send({code:1,msg:"删除成功"})
    }
  })
})
module.exports=router